package com.fz.controller;

import com.fz.entity.Artist;
import com.fz.entity.College;
import com.fz.entity.Province;
import com.fz.mapper.WebArtistMapper;
import com.fz.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Z-C-Y on 2018-03-05.
 * artist.jsp   艺术家
 */
@Controller
public class WebArtistController {
    @Autowired
    protected WebArtistMapper webArtistMapper;
    @RequestMapping(value = "/webpage/artist")
    public String artist(Model model, String seekName, HttpServletRequest req){
     /*作者省份*/
     List<Province> list = webArtistMapper.queryProvince();
     model.addAttribute("queryProvince",list);
     /*毕业美院*/
     List<College> list1 = webArtistMapper.queryCollege();
     model.addAttribute("queryCollege",list1);
     /*精准搜索==先接受页面传了的数据，向数据库进行查询后===再返回查询的数据*/
     System.out.println("===接受到的值=="+seekName);
     if(seekName !=null && !"".equals(seekName)){
         List<Artist> list2 = webArtistMapper.queryName(seekName);
         model.addAttribute("ddd",list2);
     }


     /*实现分页并查询的功能*/
        int  pageNun1 = 1;
        if((req.getParameter("pageNun") != null) && !("".equals(req.getParameter("pageNun")))){
            pageNun1 = Integer.parseInt(req.getParameter("pageNun"));
        }
        //System.out.println("==获得当前是第几页==pageNun===="+req.getParameter("pageNun"));
        //每页显示的记录数
        int pageSize = 5;
        /*分页======查询数据库的数据的总数据*/
        List<Artist> list2 = webArtistMapper.queryArtist();
        model.addAttribute("list2Artist",list2);

        /*得到表中总条数*/
        int totalRecord = list2.size();

        //有了初始化数据，就可以创建PageBean对象了
        PageBean pbm = new PageBean(pageNun1, pageSize, totalRecord);

        model.addAttribute("pbm",pbm);
        //获取PageBeanManage对象中的startIndex;
        int startIndex = pbm.getStartIndex();

        //有了pageSize和startIndex，就可以拿到每页的数据
       List<Artist> list3 =  webArtistMapper.queryData(startIndex,pageSize);
       model.addAttribute("list3",list3);
        model.addAttribute("t","artist");
     return "/webpage/artist";
    }





}
